package com.yss.system.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yss.common.core.utils.StringUtils;
import com.yss.common.core.utils.uuid.IdUtils;
import com.yss.system.mapper.SysOperLogMapper;
import com.yss.system.service.ISysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yss.system.api.domain.SysOperLog;

/**
 * 操作日志 服务层处理
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements ISysOperLogService {
    @Autowired
    private SysOperLogMapper operLogMapper;

    /**
     * 新增操作日志
     *
     * @param operLog 操作日志对象
     * @return 结果
     */
    @Override
    public int insertOperlog(SysOperLog operLog) {
        operLog.setId(IdUtils.fastSimpleUUID());
        return operLogMapper.insert(operLog);
    }

    /**
     * 查询系统操作日志集合
     *
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysOperLog> selectOperLogList(SysOperLog operLog) {
        QueryWrapper<SysOperLog> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("OPER_TIME");
        if (null == operLog) {
            return this.list();
        }
        if (StringUtils.isNotEmpty(operLog.getTitle())) {
            wrapper.like("TITLE", operLog.getTitle());
        }
        if (null != operLog.getBusinessType()) {
            wrapper.eq("BUSINESS_TYPE", operLog.getBusinessType());
        }
        if (null != operLog.getBusinessTypes()) {
            List<Integer> list = Arrays.asList(operLog.getBusinessTypes());
            wrapper.in("BUSINESS_TYPE", list);
        }
        if (null != operLog.getStatus()) {
            wrapper.eq("STATUS", operLog.getStatus());
        }
        if (StringUtils.isNotEmpty(operLog.getOperName())) {
            wrapper.like("OPER_NAME", operLog.getOperName());
        }
        if (StringUtils.isNotEmpty(operLog.getParams())) {
            if (null != operLog.getParams().get("beginTime") && StringUtils.isNotEmpty(operLog.getParams().get("beginTime").toString())) {
                wrapper.ge("OPER_TIME", operLog.getParams().get("beginTime"));
            }
            if (null != operLog.getParams().get("endTime") && StringUtils.isNotEmpty(operLog.getParams().get("endTime").toString())) {
                wrapper.le("OPER_TIME", operLog.getParams().get("endTime") + ": 23:59:59");
            }
        }
        return this.list(wrapper);
    }

    /**
     * 批量删除系统操作日志
     *
     * @param ids 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteOperLogByIds(List<String> ids) {
        return operLogMapper.deleteBatchIds(ids);
    }

    /**
     * 查询操作日志详细
     *
     * @param id 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperLog selectOperLogById(String id) {
        return operLogMapper.selectById(id);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog() {
        this.remove(new QueryWrapper<>());
    }
}
